package com.wac.data.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wac.data.entity.Dynamic;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author beta
 * @since 2023-03-16
 */
public interface DynamicMapper extends BaseMapper<Dynamic> {

	//获取某用户的动态数量
	@Select("select count(*) from dynamic where user_id = #{userId}")
	int getDynamicNum(String userId);

	// 查询所有动态 时间降序
	@Select("SELECT dynamic.*,USER.name,USER.head_url,l.likeNum,t.commentNum FROM dynamic,USER,(select dynamic.id,count(entity_id) as likeNum " +
			"from dynamic left join `like` on `like`.entity_id = dynamic.id group by dynamic.id) as l,(SELECT dynamic.id,count( `comment`.id ) AS commentNum FROM dynamic LEFT JOIN `comment` ON `comment`.comment_id = dynamic.id GROUP BY dynamic.id) AS t " +
			"WHERE t.id = dynamic.id AND l.id = dynamic.id AND USER.id=dynamic.user_id ORDER BY time DESC")
	Page<Dynamic> selectAll(Page page);

	// 查询某用户的所有动态 时间降序
	@Select("SELECT dynamic.*,USER.name,USER.head_url,l.likeNum,t.commentNum,watched.number as watchedNum " +
			"FROM dynamic,USER,(select dynamic.id,count(entity_id) as likeNum from dynamic left join `like` on `like`.entity_id = dynamic.id group by dynamic.id) as l,(SELECT dynamic.id,count( `comment`.id ) AS commentNum FROM dynamic LEFT JOIN `comment` ON `comment`.comment_id = dynamic.id GROUP BY dynamic.id) AS t,watched " +
			"WHERE t.id = dynamic.id AND l.id = dynamic.id AND USER.id=dynamic.user_id AND watched.id = dynamic.id AND dynamic.user_id = #{userId} ORDER BY time DESC")
	Page<Dynamic> selectByUserId(Page page, String userId);

	@Select("select * from dynamic where user_id = #{userId} order by time desc")
	List<Dynamic> selectByUserIdDesc(String id);

	@Select("select * from dynamic where user_id = #{userId} order by time asc")
	List<Dynamic> selectByUserIdAsc(String id);

	@Select("select dynamic.*,`user`.`name` from dynamic left join `user` on dynamic.user_id = `user`.id where dynamic.id = #{id}")
	Dynamic selectById(String id);

	// 插入动态
	@Insert("insert into dynamic (user_id,content,image_url) values(#{userId},#{content},#{imageUrl})")
	int addDynamic(String userId, String content, String imageUrl);

}
